//
//  YPWmcDrivingManager.swift
//  YPWatermarkCamera
//
//  Created by 赵新 on 2022/4/15.
//

import UIKit
import YPBedrock
import YPGraphicEngine

// MARK: - [YPWmcDrivingManager]

/// 驱动操作管理器
class YPWmcDrivingManager: NSObject {
    // Public

    /// 单例实例化
    public static let shared: YPWmcDrivingManager = {
        let manager = YPWmcDrivingManager()
        return manager
    }()

    // Internal

    // MARK: - 事务驱动

    // MARK: 首页驱动

    /// 首页 - 清理最后一次使用水印的缓存
    var home_clearWatermarkLastCacheBlock: YPBlock.Empty?
    
    /// 首页 - 刷新顶部菜单按钮
    var home_reloadTopMenuItemsBlock: YPBlock.Empty?

    /// 首页 - 刷新底部工具栏左按钮
    var home_reloadToolLeftMenuItemsBlock: YPBlock.Empty?
    /// 首页 - 刷新底部工具栏右按钮
    var home_reloadToolRightMenuItemsBlock: YPBlock.Empty?

    /// 首页 - 弹出通知视图回调
    var home_addNoticeViewBlock: YPBlock.TwoParam<UIView, String>?
    /// 首页 - 消失通知视图回调
    var home_removeNoticeViewBlock: YPBlock.OneParam<String>?

    /// 首页 - 设置水印模型
    var home_replaceWatermarkBlock: YPBlock.OneParam<WmcWatermarkDetailModel>?
    
    /// 首页 - 保存图片
    var home_savePhotoToLocalAlbumBlock: YPBlock.OneParam<YPWmcManager.PhotoResult>?
    /// 首页 - 上传视频到云相册
    var home_uploadVideoToLocalAlbumBlock: YPBlock.OneParam<YPWmcManager.VideoResult>?
    
    /// 首页 - 向相机控制视图添加提示视图
    var home_addCameraControlTipsBlock: YPBlock.TwoParam<String, UIView>?
    /// 首页 - 向相机控制视图添加提示视图
    var home_removeCameraControlTipsBlock: YPBlock.OneParam<String>?
    /// 首页 - 移除相机控制视图的所有添加视图
    var home_removeAllCameraControlTipsBlock: YPBlock.Empty?
    
    /// 首页 - 拍照事务
    var home_takePhotoActionBlock: YPBlock.Empty?
    /// 水印列表
    var home_showWatermarkListActionBlock: YPBlock.Empty?
    /// 数钢筋弹窗列表
    var home_showObjectRecognitionAlertView: YPBlock.Empty?
    /// 切换镜头
    var home_shotCutActionBlock: YPBlock.Empty?
    /// 设置前后镜头
    var home_setCameraPosition: YPBlock.OneParam<YPGECamera.Position>?
    /// 切换功能模式
    var home_switchFunctionMode: YPBlock.OneParam<WmcButtonItemType.HomeFunctionMode>?
    
    // MARK: 媒体浏览驱动

    /// 媒体浏览页刷新底部工具栏
    var mediaBrowse_reloadToolMenuItemsBlock: YPBlock.OneParam<WmcMedia.MediaType>?
    /// 媒体浏览页添加气泡
    var mediaBrowse_addTooMenuPopBlock: YPBlock.TwoParam<WmcBubbleVM, Int>?
    /// 媒体浏览页 获取当前媒体的水印信息
    var mediaBrowse_currentMediaWatermarkBlock: YPBlock.Return<WmcWatermarkDetailModel?>?

    // MARK: 相机设置驱动

    /// 相机设置页刷新
    var cameraSetting_reloadTableDataBlock: YPBlock.Empty?

    // MARK: 预览页驱动

    /// 预览页 - 混合图片
    var photoPreview_blendPhotoBlock: YPBlock.Empty?
    
    /// 预览页 - 保存图片
    var photoPreview_savePhotoToLocalAlbumBlock: YPBlock.OneParam<YPWmcManager.PhotoResult>?

    // MARK: 水印编辑驱动

    /// 水印编辑页刷新
    var watermarkEdit_reloadTableDataBlock: YPBlock.Empty?
    /// 水印编辑页修改水印条目
    var watermarkEdit_changeWatermarkOptionBlock: YPBlock.TwoParam<Int, WmcWatermarkOptionModel>?
    /// 水印编辑页更新水印
    var watermarkEdit_reloadWatermarkModelBlock: YPBlock.OneParam<WmcWatermarkDetailModel>?

    // MARK: 云相册驱动
    

    // MARK: 自定义页面驱动

    /// 自定义 - 设置标题
    var custom_setTitleBlock: YPBlock.OneParam<String>?
    /// 自定义 - 返回页面
    var custom_backViewControllerBlock: YPBlock.Empty?
    /// 自定义 - 推出页面
    var custom_pushCustomViewBlock: YPBlock.OneParamReturn<WmcCustomViewProtocol, UIViewController?>?
    /// 自定义 - 路由推出页面
    var custom_pushNavigationBlock: YPBlock.FiveParamReturn<WmcNavigatorManager.Path, WmcTransition.Style?, Any?, Bool, YPBlock.Empty?, UIViewController?>?
}

// MARK: - 接口驱动

extension YPWmcDrivingManager {
    /// 用户是否是第一次拍照
    /// - Parameter block: 结果回调
    static func userTakePhotoIsFirst(_ block: @escaping YPBlock.OneParam<Bool>) {
        WmcOtherAPI.fetchFirstTakePhotoStatus { response in
            if let dict = response.data as? [String: Any], let isFirst = dict["isFirst"] as? Int {
                let result = isFirst != 0
                block(result)
            }
        } failure: { error in
            block(false)
        }
    }
}
